package com.hitqz.robot.biz.flow.component.patrol;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.dto.robot.RobotGoTargetPosDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolTicketsDto;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@LiteflowComponent(value = "GoTargetDevPosition", name = "[巡检]前往设备点")
@AllArgsConstructor
public class GoTargetDevPosition extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final DispatchService dispatchService;


    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        if (isNeedSkip(dto)){
            log.info("跳过当前步骤");
            return;
        }
        TicketsTask task = dto.getTicketsTask();
        FlowPatrolTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolTicketsDto.class);
        FlowPatrolPreAndEndTicketsDto endParams = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        if (params.getPosId()==null && endParams.getEndPosId()==null){
            log.warn("没有站点信息,不执行");
            return;
        }
        Integer posId = params.getPosId() != null ? params.getPosId() : endParams.getEndPosId();
        RobotMapPos robotMapPos = robotMapPosService.getById(posId);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        if (Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached()) {
            log.info("已在设备点{},直接执行动作",robotMapPos.getCode());
        } else {
            log.info("前往设备点{}", robotMapPos.getCode());
            //调用规划路径算法的前往站点
            dispatchService.go2TargetPosition(robotMapPos, false);
            TimeUnit.SECONDS.sleep(1);
            iRobotPosDto = robotDriverService.getWebPos();
            while ( !(Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached())) {
                iRobotPosDto = robotDriverService.getWebPos();
                TimeUnit.SECONDS.sleep(1);
                //外部中断标志
                LiteFlowUtil.checkInterruptFlag();
            }
            log.info("机器人到达位置了");
        }
        log.info("到达点位坐标 {}---- x: {}, y: {}, yaw {}", iRobotPosDto.getCurrentStation(),
                iRobotPosDto.getX(), iRobotPosDto.getY(), iRobotPosDto.getAngle());
    }


    private boolean isNeedSkip(FlowContextDto dto){
        try {
            TicketsTask lastTicketsTask = dto.getLastTicketsTask();
            if (lastTicketsTask == null) {
                return false;
            }
            //当前参数
            TicketsTask task = dto.getTicketsTask();
            FlowPatrolTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolTicketsDto.class);
            FlowPatrolPreAndEndTicketsDto endParams = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
            Integer currentTargetPosId = params.getPosId() != null ? params.getPosId() : (endParams.getEndPosId() != null ? endParams.getEndPosId() : null);
            //上一次的参数
            FlowPatrolTicketsDto lastParams = JSONObject.parseObject(lastTicketsTask.getParseOrderParams(), FlowPatrolTicketsDto.class);
            FlowPatrolPreAndEndTicketsDto lastEndParams = JSONObject.parseObject(lastTicketsTask.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
            Integer lastTargetPosId = lastParams.getPosId() != null ? lastParams.getPosId() : (lastEndParams.getEndPosId() != null ? lastEndParams.getEndPosId() : null);
            if (params.getPosId()!=null && Objects.equals(params.getPosId(),lastParams.getPosId()) ){
                log.info("当前工单任务与上一次工单任务目标点相同:{}", currentTargetPosId);
                return true;
            }
            if(endParams.getEndPosId() != null && Objects.equals(endParams.getEndPosId(),lastEndParams.getEndPosId())){
                log.info("当前工单任务与上一次工单任务目标点相同:{}", currentTargetPosId);
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }


}